package team.pikachu.pikachuclient.message;

import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import team.pikachu.pikachuclient.R;
import team.pikachu.pikachuclient.beans.Trouble;
import team.pikachu.pikachuclient.utils.Constants;
import team.pikachu.pikachuclient.utils.HttpUtils;
import team.pikachu.pikachuclient.utils.IPConstants;
import team.pikachu.pikachuclient.utils.LogUtil;

public class MsgDetailActivity extends AppCompatActivity {
    private final String LOGTAG = LogUtil.makeLogTag(MsgDetailActivity.class);
    private SharedPreferences pre;
    private int userId;
    private int troubleId;

    @BindView(R.id.show_event)
    TextView mEventText;
    @BindView(R.id.show_location)
    TextView mLocationText;
    @BindView(R.id.show_upload)
    TextView mTimeText;
    @BindView(R.id.show_detail)
    TextView mDetailText;
    @BindView(R.id.show_state)
    TextView mState;
    @BindView(R.id.title_finish)
    TextView mFinishTitleText;
    @BindView(R.id.show_finish)
    TextView mFinishText;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.finished)
    Button mFinishedBtn;

    @OnClick(R.id.finished)
    void onFinish() {
        FinishAsy finishAsy = new FinishAsy();
        String finishPath = IPConstants.updateStatus(userId,troubleId,Constants.OP_FINISED);
//        Log.e(LOGTAG,finishPath);
        finishAsy.execute(finishPath);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trouble_detail);
        Trouble trouble = (Trouble) getIntent().getSerializableExtra("TROUBLE");
        troubleId = trouble.getId();

        Log.d(LOGTAG, trouble.toString());
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        String status = "";
        int color = 0;
        switch (trouble.getState()) {
            case Constants.STATUS_WAITING:
                status = "等待中";
                color = R.color.status_waiting;
                break;
            case Constants.STATUS_DOING:
                status = "维修中";
                color = R.color.status_doing;
                break;
            case Constants.STATUS_FINISHED:
                status = "已维修";
                color = R.color.status_finished;
                break;
        }

        pre = getSharedPreferences("userInfo", MODE_PRIVATE);
        int roleId = pre.getInt(getString(R.string.pre_roleid), 1);
        userId = pre.getInt(getString(R.string.pre_userid),0);

        if (roleId == Constants.ROLE_WORKER && !status.equals("已维修")) {
            mFinishedBtn.setVisibility(View.VISIBLE);
        } else if(status.equals("已维修")){
            mFinishText.setVisibility(View.VISIBLE);
            mFinishTitleText.setVisibility(View.VISIBLE);
            mFinishText.setText(trouble.getFinishtime());
        }

        mEventText.setText(trouble.getTruble());
        mLocationText.setText(trouble.getLocation().getLocation() + " " + trouble.getLocation_1());
        mTimeText.setText(trouble.getTime());
        mDetailText.setText(trouble.getMessage());
        mState.setText(status);
        mState.setTextColor(getResources().getColor(color));
    }

    private class FinishAsy extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            return HttpUtils.sendPostMethod(params[0]);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s.equals(getString(R.string.request_failed)) || s.equals(getString(R.string.timeout))) {
                Toast.makeText(MsgDetailActivity.this, "发送失败", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MsgDetailActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
                mFinishedBtn.setVisibility(View.GONE);
                mState.setText("已维修");
                mFinishTitleText.setVisibility(View.VISIBLE);
                mFinishText.setVisibility(View.VISIBLE);
                Date date = new Date();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                String d = format.format(date);
                mFinishText.setText(d);
                mState.setTextColor(getResources().getColor(R.color.status_finished));
            }
        }
    }

}
